package com.woniuxy03;

import java.util.Arrays;
import java.util.Comparator;

public class Test03 {
    public static void main(String[] args) {
        //对象数组
        Student[] students = new Student[5];
        students[0] = new Student("zs", 11);
        students[1] = new Student("ls", 13);
        students[2] = new Student("ww", 19);
        students[3] = new Student("zl", 16);
        students[4] = new Student("aaa", 10);
        System.out.println("排序前");
        System.out.println(Arrays.toString(students));
        System.out.println("Comparable默认排序--自然排序,sort(数组参数)，底层使用Comparable接口中的compartor(o)比较");
        Arrays.sort(students);
        System.out.println(Arrays.toString(students));
        System.out.println("Comparator--自定义排序，使用姓名降序排列");
        Arrays.sort(students, new NameDescComprator());
        System.out.println(Arrays.toString(students));
        System.out.println("Comparator--自定义排序，使用年龄升序");
        Arrays.sort(students, new AgeAscComprator());
        System.out.println(Arrays.toString(students));
        System.out.println("Comparator--自定义排序，使用年龄降序");
        Arrays.sort(students, new Comparator<Student>() {
            @Override
            public int compare(Student o1, Student o2) {
                return o2.getAge() - o1.getAge();
            }
        });
        System.out.println(Arrays.toString(students));

//        Comparator comparator1 = new NameDescComprator();
//        //匿名内部类
//        //new 接口实现类对象，并没有定义类
//        Comparator comparator2 = new Comparator() {
//            @Override
//            public int compare(Object o1, Object o2) {
//                return 0;
//            }
//        };

    }
}
